﻿using MediaFoundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CameraMF.NET6
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml 
    /// 特别说明 这里的两个DLL MediaFoundation.DLL MediaFoundation.Extension.DLL 
    /// 来自https://sourceforge.net/projects/mfnet/
    /// </summary>
    public partial class MainWindow : Window
    {
#if Sync
        private MFCaptureSync mfcapsync;
#else
        private MFCaptureAsync mfcapasync;
        private ImageSource imagesource;
#endif
        private DispatcherTimer timer = new DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded; ;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //Width = SystemParameters.PrimaryScreenWidth;
            //Height = SystemParameters.PrimaryScreenHeight;
            //Left = 0;
            //Top = 0;

#if Sync
            mfcapsync = new MFCaptureSync();
            mfcapsync.OpenCaptureDevice();
            mfcapsync.SetupCatureType(1280, 720, 30, "MJPG");
            mfcapsync.CaptureStart();

            timer.Interval = TimeSpan.FromMilliseconds(30);
            timer.Tick += TimerTick;
            timer.Start();
#else
            MFDevice device = MFDevice.GetVideoCaptureDevices()[0];
            mfcapasync = new MFCaptureAsync(Dispatcher);
            mfcapasync.OnCaptureEvent += OnCapture;
            mfcapasync.SetDevice(device, 3840, 2160, 30, "MJPG");
            mfcapasync.Request();
#endif
        }

        private int timecounter;
        private DateTime lasttime = DateTime.Now;

#if Sync

        private void TimerTick(object sender, EventArgs e)
        {
            ImageSource imgsource = mfcapsync.Capture();
            cameraframe.Background = new ImageBrush(imgsource);
            timecounter++;

            if ((DateTime.Now - lasttime).TotalMilliseconds > 1000)
            {
                Console.WriteLine("FPS:" + timecounter.ToString());
                timecounter = 0;
                lasttime = DateTime.Now;
            }
        }
#endif

#if !Sync
        private void OnCapture(object? sender, ImageSource e)
        {
            cameraframe.Background = new ImageBrush(e);
            mfcapasync.Request();
            timecounter++;

            if ((DateTime.Now - lasttime).TotalMilliseconds > 1000)
            {
                Console.WriteLine("FPS:" + timecounter.ToString());
                timecounter = 0;
                lasttime = DateTime.Now;
            }
        }
#endif
    }
}
